﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using winproject.Models;
using Newtonsoft.Json;
using System.Data.Linq;
using System.IO;
using winproject;
namespace winproject.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        HelperController oHelper = new HelperController();

        public ActionResult Index()
        {
            return View();
        }

        //########################### CATEGORY ##############################

        /// <summary>
        /// Category
        /// </summary>
        /// <param name="oSearchItem"></param>
        /// <returns></returns>
        public ActionResult Category(CategoryDetails oSearchItem = null)
        {
            if (Request.Form.HasKeys())
            {
                //check if data is avaible in post
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oSearchItem = new CategoryDetails();
                    oSearchItem.nItemId = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
            }
            if (oSearchItem != null && oSearchItem.nItemId > 0)
            {
                ReturnResult oResult=null;
                List<CategoryDetails> oItemDetailList = oHelper.GetCategoryDetails(out oResult, oSearchItem);
                var x = oResult;


                ViewData["resCode"] = x.errCode;
                ViewData["resData"] = (oItemDetailList != null && oItemDetailList.Count > 0) ? oItemDetailList[0] : null;
            }
            return View();
        }

        #region Category Details Add Section
        [HttpPost, ValidateInput(false)]
        public ActionResult AddCategory()
        {
            ReturnResult oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.BadRequest, msgJson = "[]" };
            //string path = Server.MapPath("~/Content/VirusUploads");
            CategoryDetails oItemDetail = new CategoryDetails();
            if (Request.Form.HasKeys())
            {


                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oItemDetail.nItemId = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
                if (Request.Form.AllKeys.Contains("txtCatName"))
                {
                    oItemDetail.sItemName = Convert.ToString(Request.Form["txtCatName"]);
                }
                if (Request.Form.AllKeys.Contains("txtCatSlug"))
                {
                    oItemDetail.sItemSlug = Convert.ToString(Request.Form["txtCatSlug"]);
                }
                if (Request.Form.AllKeys.Contains("txtCatDesc"))
                {
                    oItemDetail.sItemDescription = HttpUtility.HtmlEncode(Convert.ToString(Request.Form["txtCatDesc"]));
                }
                if (oItemDetail != null && !string.IsNullOrEmpty(oItemDetail.sItemName) && !string.IsNullOrEmpty(oItemDetail.sItemDescription))
                {
                    CategoryModel oItemModel = new CategoryModel();
                    if (oItemDetail.nItemId > 0)
                    {
                        //edit request
                        oResult = oItemModel.AddNewCategory(oItemDetail, true);
                    }
                    else
                        oResult = oItemModel.AddNewCategory(oItemDetail);
                }
                else
                {
                    oResult.errCode = (int)cEnums.eErrorCodes.OperationFailed;
                    oResult.msgJson = "Please provide virus details";
                }
            }

            var globalModel = new GlobalViewModel()
            {
                ApplicationName = Convert.ToString(HttpContext.Application["SiteName"]),
                jsonServerString = JsonConvert.SerializeObject(oResult)

            };
            return RedirectToAction("Category", oResult);
        }

        #endregion

        #region Get Category Detail JSON String - Service (Ajax) Call
        public JsonResult GetCategoryDetailJson(CategoryDetails oSearchItem)
        {
            CategoryModel oItemDetail = new CategoryModel();
            List<CategoryDetails> oIDetailList = null;
            ReturnResult oResult = oItemDetail.getCategoryDetails(out oIDetailList, oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Delete Category - Service (Ajax) Call
        public JsonResult DeleteCategory(CategoryDetails oSearchItem)
        {
            CategoryModel oItemDetail = new CategoryModel();
            ReturnResult oResult = oItemDetail.deleteCategory(oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        //########################### SUBCATEGORY ##############################

        /// <summary>
        /// SubCategory()
        /// </summary>
        /// <param name="oSearchItem"></param>
        /// <returns></returns>
        public ActionResult SubCategory(SubCategoryDetails oSearchItem = null)
        {
            if (Request.Form.HasKeys())
            {
                //check if data is avaible in post
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oSearchItem = new SubCategoryDetails();
                    oSearchItem.nItemId = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
            }
            if (oSearchItem != null && oSearchItem.nItemId > 0)
            {
                ReturnResult oResult = null;
                List<SubCategoryDetails> oItemDetailList = oHelper.GetSubCategoryDetails(out oResult, oSearchItem);
                var x = oResult;


                ViewData["resCode"] = x.errCode;
                ViewData["resData"] = (oItemDetailList != null && oItemDetailList.Count > 0) ? oItemDetailList[0] : null;
            }

            ReturnResult oCatResResult = null;
            List<CategoryDetails> lsCatList = oHelper.GetCategoryDetails(out oCatResResult, null);

            if (oCatResResult!=null && oCatResResult.errCode == (int)cEnums.eErrorCodes.Success)
            {
                ViewData["Categories"] = lsCatList;
            }
            else
            {
                ViewData["Categories"] = null;
            }
            return View();
        }

        #region SubCategory Details Add Section
        [HttpPost, ValidateInput(false)]
        public ActionResult AddSubCategory()
        {
            ReturnResult oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.BadRequest, msgJson = "[]" };
            //string path = Server.MapPath("~/Content/VirusUploads");
            SubCategoryDetails oItemDetail = new SubCategoryDetails();
            if (Request.Form.HasKeys())
            {


                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oItemDetail.nItemId = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
                if (Request.Form.AllKeys.Contains("drpCategory"))
                {
                    oItemDetail.nParentId = Convert.ToInt32(Request.Form["drpCategory"]);
                }
                if (Request.Form.AllKeys.Contains("txtSubCatName"))
                {
                    oItemDetail.sItemName = Convert.ToString(Request.Form["txtSubCatName"]);
                }
                if (Request.Form.AllKeys.Contains("txtSubCatSlug"))
                {
                    oItemDetail.sItemSlug = Convert.ToString(Request.Form["txtSubCatSlug"]);
                }
                if (Request.Form.AllKeys.Contains("txtSubCatDesc"))
                {
                    oItemDetail.sItemDescription = HttpUtility.HtmlEncode(Convert.ToString(Request.Form["txtSubCatDesc"]));
                }
                if (oItemDetail != null && oItemDetail.sItemName.Trim().Length > 0 && oItemDetail.sItemDescription.Trim().Length > 0)
                {
                    SubCategoryModel oItemModel = new SubCategoryModel();
                    if (oItemDetail.nItemId > 0)
                    {
                        //edit request
                        oResult = oItemModel.AddNewSubCategory(oItemDetail, true);
                    }
                    else
                        oResult = oItemModel.AddNewSubCategory(oItemDetail);
                }
                else
                {
                    oResult.errCode = (int)cEnums.eErrorCodes.OperationFailed;
                    oResult.msgJson = "Please provide sub category details";
                }
            }

            var globalModel = new GlobalViewModel()
            {
                ApplicationName = Convert.ToString(HttpContext.Application["SiteName"]),
                jsonServerString = JsonConvert.SerializeObject(oResult)

            };
            return RedirectToAction("SubCategory", oResult);
        }

        #endregion

        #region Get SubCategory Detail JSON String - Service (Ajax) Call
        public JsonResult GetSubCategoryDetailJson(SubCategoryDetails oSearchItem)
        {
            SubCategoryModel oItemDetail = new SubCategoryModel();
            List<SubCategoryDetails> oIDetailList = null;
            ReturnResult oResult = oItemDetail.getSubCategoryDetails(out oIDetailList, oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Delete SubCategory - Service (Ajax) Call
        public JsonResult DeleteSubCategory(SubCategoryDetails oSearchItem)
        {
            SubCategoryModel oItemDetail = new SubCategoryModel();
            ReturnResult oResult = oItemDetail.deleteSubCategory(oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

   
        //########################### VIRUS ##############################
        public ActionResult Virus(VirusDetails oSearchItem=null)
        {
            if (Request.Form.HasKeys())
            {
                //check if data is avaible in post
                if (Request.Form.AllKeys.Contains("txtVirusId"))
                {
                    oSearchItem = new VirusDetails();
                    oSearchItem.nVirusId= Convert.ToInt32(Request.Form["txtVirusId"]);
                }
            }
            if (oSearchItem != null && oSearchItem.nVirusId > 0)
            {
                VirusModel oVirusDetail = new VirusModel();
                List<VirusDetails> oVDetailList = null;
                ReturnResult oResult = oVirusDetail.getVirusDetails(out oVDetailList,oSearchItem);
                 var x = oResult;
                
                
                 ViewData["resCode"]= x.errCode;
                ViewData["resData"] =(oVDetailList!=null && oVDetailList.Count>0)?oVDetailList[0]:null;
            }   
            return View();
        }


        #region Virus Details Upload Section
        [HttpPost, ValidateInput(false)]
        public ActionResult VirusUpload()
        {
            ReturnResult oResult = new ReturnResult() { errCode=(int)cEnums.eErrorCodes.BadRequest,msgJson="[]" };
            string path = Server.MapPath("~/Content/VirusUploads");
            VirusDetails oVirus = new VirusDetails();
            if (Request.Form.HasKeys())
            {


                if (Request.Form.AllKeys.Contains("txtVirusId"))
                {
                    oVirus.nVirusId = Convert.ToInt32(Request.Form["txtVirusId"]);
                }
                if (Request.Form.AllKeys.Contains("txtVirusName"))
                {
                    oVirus.sVirusName = Convert.ToString(Request.Form["txtVirusName"]);
                }
                if (Request.Form.AllKeys.Contains("txtVirusSlug"))
                {
                    oVirus.sVirusSlug = Convert.ToString(Request.Form["txtVirusSlug"]);
                }
                if (Request.Form.AllKeys.Contains("txtVirusDesc"))
                {
                    oVirus.sVirusDescription = HttpUtility.HtmlEncode(Convert.ToString(Request.Form["txtVirusDesc"]));
                }
                if (Request.Files.AllKeys.Contains("viruspic"))
                {
                    HttpPostedFileBase file = Request.Files["viruspic"];
                    if (file != null && file.ContentLength > 0)
                    {
                        string fname = Path.GetFileName(file.FileName);
                        fname = string.Format("{0}{1}", oVirus.sVirusName, Path.GetExtension(fname));
                        string uploadPath = Path.Combine("~/Content/VirusUploads/", fname);

                        string fpath = Server.MapPath(uploadPath);
                        if (!System.IO.Directory.Exists(fpath.Replace(fname,"")))
                        {
                            Directory.CreateDirectory(fpath.Replace(fname, ""));
                        }
                        if (System.IO.Directory.Exists(fpath.Replace(fname, "")))
                        {
                            file.SaveAs(fpath);
                            if (System.IO.File.Exists(fpath))
                            {
                                oVirus.sVirusImagePath = uploadPath;
                            }
                        }
                        else
                        {
                            oVirus.sVirusImagePath = "Error";
                        }
                    }
                }
                if (oVirus != null && oVirus.sVirusName.Trim().Length > 0 && oVirus.sVirusDescription.Trim().Length>0)
                {
                    VirusModel oVirusModel = new VirusModel();
                    if (oVirus.nVirusId > 0)
                    {
                        //edit request
                        oResult = oVirusModel.AddNewVirus(oVirus,true);
                    }
                    else
                        oResult = oVirusModel.AddNewVirus(oVirus);
                }
                else
                {
                    oResult.errCode = (int)cEnums.eErrorCodes.OperationFailed;
                    oResult.msgJson = "Please provide virus details";
                }
            }
            
            var globalModel = new GlobalViewModel()
            {
                ApplicationName =Convert.ToString(HttpContext.Application["SiteName"]),
                jsonServerString=JsonConvert.SerializeObject(oResult)
                
            };
            return RedirectToAction("virus", oResult);
        }

        #endregion

        #region Get Virus Detail JSON String - Service (Ajax) Call
        public JsonResult GetVirusDetailJson(VirusDetails oSearchItem)
        {
            VirusModel oVirusDetail = new VirusModel();
            List<VirusDetails> oVDetailList = null;
            ReturnResult oResult = oVirusDetail.getVirusDetails(out oVDetailList,oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Delete Virus - Service (Ajax) Call
        public JsonResult DeleteVirus(VirusDetails oSearchItem)
        {
            VirusModel oVirusDetail = new VirusModel();
            ReturnResult oResult = oVirusDetail.deleteVirus(oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

       

        //########################### Countries ##############################

        public ActionResult Countries()
        {
            return View();
        }

        //########################### Currencies ##############################

        public ActionResult Currencies()
        {
            return View();
        }

        //########################### Country regions ##############################

        public ActionResult CountryRegions()
        {
            return View();
        }

        //########################### Countries ##############################
        public ActionResult tets()
        {
            var globalModel = new GlobalViewModel()
            {
                ApplicationName = HttpContext.Application["SiteName"] as string
            };
            return View(globalModel);
        }
    }
}
